const { updateOneDrama,deleteFile } = require("../services/dramaServices");



const uploadCoverImage = async (req, res) => {
    try {
        deleteFile(req.body._id,"cover");
        const url = req.protocol + "://" + "api.gronaki.top"
        var content = url + "/public/drama_Cover/" + req.file.filename
        const dramaImage = await updateOneDrama(req.body._id, "cover", content);

        if (dramaImage == null) {
            return res.status(400).json({ error_msg: "未找到该条数据" });
        }
        else if (dramaImage == 1) {
            return res.status(400).json({ error_msg: "字段错误" });
        }
        else if (dramaImage == 2) {
            return res.status(400).json({ error_msg: "数据错误" });
        }
        else {
            return res.status(200).json({ success: dramaImage });
        }
    }
    catch (err) {
        return res.status(500).json({ error_msg: err.message });
    }
}

module.exports = { uploadCoverImage }